//Based on the tutorial at:
//http://artis.imag.fr/Membres/Xavier.Decoret/resources/ode/tutorial1.html

#include "Object.h"

Object::Object()
{}

Object::Object(std::vector<Vertex> &verts, std::vector<GLuint> &indices) : vertices(verts), indices(indices)
{}

Object::~Object()
{}

//Renders an object using OpenGL
void Object::render() const
{
	glPushMatrix();

	const dReal *pos = dBodyGetPosition(body);

	//The rotation matrix from ODE is 3 x 4, but the 4th column should be ignored AFAIK
	const dReal *rot = dBodyGetRotation(body);

	float mat[16];

	//Need to transpose elements of the rotation matrix as it's in column-major order
	//and open GL uses row-major
	mat[ 0] = rot[ 0]; mat[ 1] = rot[ 4]; mat[ 2] = rot[ 8]; mat[ 3] = 0;
	mat[ 4] = rot[ 1]; mat[ 5] = rot[ 5]; mat[ 6] = rot[ 9]; mat[ 7] = 0;
	mat[ 8] = rot[ 2]; mat[ 9] = rot[ 6]; mat[10] = rot[10]; mat[11] = 0;
	mat[12] = pos[ 0]; mat[13] = pos[ 1]; mat[14] = pos[ 2]; mat[15] = 1;

	//Rotate and translate object accordingly
	glMultMatrixf(mat);

	renderInLocalFrame();

	glPopMatrix();
}
const dReal * Object::getPosition()
{
	return dBodyGetPosition(body);

}

//Sets position of object in ODE world
void Object::setPosition(dReal x, dReal y, dReal z)
{
	//Equivalent to dGeomSetPosition(geom,x,y,z);
	dBodySetPosition(body, x, y, z);
}

